1 using System;
2 using
System.Collections;
3 using
System.Collections.Generic;
4 using
UnityEngine;
5 using
UnityEngine.UI;
6
7 public
class UIController : MonoBehaviour {
8
9     
private Text textTotalScoreOnHUD;
10     
private Text textLevelScoreOnHUD;
11     
private Text textForwardOnHUD;
12     
private Text textMoveOnHUD;
13     
private Text textScoreOnHUD;
14     
private Text textTimeBonusOnHUD;
15
16     
// Use this for initialization
17     
void Start () {
18         textTotalScoreOnHUD = GameObject.Find(
"Canvas/TextTotalScore").GetComponent<Text>();
19         textLevelScoreOnHUD = GameObject.Find(
"Canvas/TextLevelScore").GetComponent<Text>();
20         textForwardOnHUD = GameObject.Find(
"Canvas/TextForward").GetComponent<Text>();
21         textMoveOnHUD = GameObject.Find(
"Canvas/TextMove").GetComponent<Text>();
22         textScoreOnHUD = GameObject.Find(
"Canvas/TextScore").GetComponent<Text>();
23         textTimeBonusOnHUD = GameObject.Find(
"Canvas/TextTimeBonus").GetComponent<Text>();
24
25         textTotalScoreOnHUD.text =
"Total Score: " + DataDictionary.getDD().gettotalScore();
26         textLevelScoreOnHUD.text =
"Current Level Score: ";
27         textForwardOnHUD.text =
"Press the 'w' key to ski";
28         textMoveOnHUD.text =
"Steer using the mouse and 'a' and 'd' keys";
29         textScoreOnHUD.text =
"Steer through flags and pick up snowballs to earn points";
30         textTimeBonusOnHUD.text =
"Time Bonus: ";
31         textForwardOnHUD.enabled =
false;
32         textMoveOnHUD.enabled =
false;
33         textScoreOnHUD.enabled =
false;
34     }
35     
36     
// Update is called once per frame
37     
void Update () {
38
39         
// Game is running
40         
if (DataDictionary.getDD().getisPlaying())
41         {
42             textLevelScoreOnHUD.text =
"Current Level Score: " + DataDictionary.getDD().getlevel1Score();
43             updateTimeBonus();
44  
45             
if (!DataDictionary.getDD().gethasShownInstructions())
46             {
47                 forward();
48                 DataDictionary.getDD().sethasShownInstructions(
true);
49             }
50         }
else
51         {
52             
// Game is not running
53             textTotalScoreOnHUD.text =
"Total Score: " + DataDictionary.getDD().gettotalScore();
54             textTimeBonusOnHUD.enabled =
false;
55             textLevelScoreOnHUD.enabled =
false;
56
57             textMoveOnHUD.text =
"Press 'q' to quit";
58             textMoveOnHUD.enabled =
true;
59             
60             StartCoroutine(
"endCoroutine");
61         }
62     }
63
64     IEnumerator endCoroutine()
65     {
66         
while (!Input.GetKeyDown("q"))
67             
yield return null;
68         Debug.Log(
"Quitting app");
69         Application.Quit();
70     }
71
72     
private void updateTimeBonus()
73     {
74         
int t = (int) Math.Round(100 - 2*Time.timeSinceLevelLoad);
75
76         textTimeBonusOnHUD.text =
"Time Bonus: " + t;
77         DataDictionary.getDD().settimeBonus(t);
78     }

79
80     ///
<summary>
81     ///
Displays information on how to move forward
82     ///
</summary>
83     
private void forward()
84     {
85         textForwardOnHUD.enabled =
true;
86         StartCoroutine(
"forwardCoroutine");
87     }
88
89     IEnumerator forwardCoroutine()
90     {
91         
while (!Input.GetKeyDown("w"))
92             
yield return null;
93         textForwardOnHUD.enabled =
false;
94         StartCoroutine(
"moveCoroutine");
95     }

96
97     ///
<summary>
98     ///
Displays moving directions for 5 seconds
99     ///
</summary>
100     ///
<returns></returns>
101     IEnumerator moveCoroutine()
102     {
103         textMoveOnHUD.enabled =
true;
104         
yield return new WaitForSeconds(5);
105         StartCoroutine(
"scoreCoroutine");
106         textMoveOnHUD.enabled =
false;
107     }

108
109     ///
<summary>
110     ///
Displays how to score points
111     ///
</summary>
112     ///
<returns></returns>
113     IEnumerator scoreCoroutine()
114     {
115         textScoreOnHUD.enabled =
true;
116         
yield return new WaitForSeconds(5);
117         textScoreOnHUD.enabled =
false;
118     }
119 }


Gõ tìm kiếm nhanh...